package persistent.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import basic.HibBaseDAO;
import basic.HibernateSessionFactory;
import model.TFrontAnswer;
import model.TFrontEvaluation;
import persistent.dao.IFrontUserEvaluationDAO;
import util.toolkit.GenerateUUID;
import util.toolkit.LogUtil;

import org.hibernate.Session;
import org.hibernate.Transaction;


/**
 * 网站用户测评业务接口实现
 * @author 徐震霖
 * @since 2019-02-28 10:22
 */
public class FrontUserEvaluationImpl implements IFrontUserEvaluationDAO {

	private HibBaseDAO dao = null;
	private static final Log log = LogFactory.getLog(HibBaseDAO.class);

	public FrontUserEvaluationImpl() {
		dao = new HibBaseDAO();
	}
	
	/**
	 * 添加一次网站用户测评信息（包括用户答案添加）
	 * @param evaluation evaluation 记录对象
	 * @param answers 用户答案对象列表
	 * @return 成功返回true，失败返回null
	 */
	@Override
	public boolean addEvaluationInfo(TFrontEvaluation evaluation, List<TFrontAnswer> answers) {
		
		GenerateUUID UUID = new GenerateUUID();

		Session session = HibernateSessionFactory.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			String evaluationid = UUID.getUUID();
			evaluation.setEvaluationid(evaluationid);
			session.save(evaluation);
			for (TFrontAnswer answer : answers) {
				answer.setAnswerid(UUID.getUUID());
				answer.setEvaluationid(evaluationid);
				session.save(answer);
			}
			tx.commit();
			session.close(); // 关闭连接
			return true;
		}
		catch (Exception e) {
			if (tx != null) tx.rollback();
			session.close(); // 关闭连接
			log.error("persistent.impl.FrontUserEvaluationImpl类的addEvaluationInfo(TFrontEvaluation evaluation, List<TFrontAnswer> answers)方法调用错误：");
			log.error(LogUtil.getMsg(e));
			System.out.println("persistent.impl.FrontUserEvaluationImpl类的addEvaluationInfo(TFrontEvaluation evaluation, List<TFrontAnswer> answers)方法调用错误：");
			e.printStackTrace();
		}
		
		return false;
	}

}
